package cn.yuanqiao.archive.management.service.impl;

import cn.yuanqiao.archive.management.domain.YqZtlx;
import cn.yuanqiao.archive.management.mapper.YqZtlxMapper;
import cn.yuanqiao.archive.management.service.IYqZtlxService;
import cn.yuanqiao.common.utils.DateUtils;
import cn.yuanqiao.common.utils.StringUtils;
import cn.yuanqiao.common.utils.SecurityUtils;
import cn.yuanqiao.common.core.domain.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 载体类型Service业务层处理
 * 
 * @author sy
 * @date 2023-07-07
 */
@Service
public class YqZtlxServiceImpl implements IYqZtlxService 
{
    @Autowired
    private YqZtlxMapper yqZtlxMapper;

    /**
     * 查询载体类型
     * 
     * @param ztlxId 载体类型主键
     * @return 载体类型
     */
    @Override
    public YqZtlx selectYqZtlxByZtlxId(Long ztlxId)
    {
        YqZtlx yqZtlx = new YqZtlx();
        yqZtlx.setZtlxId(ztlxId);
        return yqZtlxMapper.selectYqZtlxByZtlxId(yqZtlx);
    }

    /**
     * 查询载体类型列表
     * 
     * @param yqZtlx 载体类型
     * @return 载体类型
     */
    @Override
    public List<YqZtlx> selectYqZtlxList(YqZtlx yqZtlx)
    {
        return yqZtlxMapper.selectYqZtlxList(yqZtlx);
    }

    /**
     * 查询所有载体类型
     *
     * @param yqZtlx 载体类型
     * @return 载体类型
     */
    @Override
    public List<YqZtlx> selectYqZtlxListAll(YqZtlx yqZtlx)
    {
        return yqZtlxMapper.selectYqZtlxListAll(yqZtlx);
    }

    /**
     * 新增载体类型
     * 
     * @param yqZtlx 载体类型
     * @return 结果
     */
    @Override
    public String insertYqZtlx(YqZtlx yqZtlx)
    {
        int num=0;
        StringBuilder successMsg = new StringBuilder();
        if (yqZtlxMapper.isOnly(yqZtlx)!=0){
            successMsg.append("名称：”"+yqZtlx.getZtlxName()+"”已存在！");
        }else {
            LoginUser user = SecurityUtils.getLoginUser();

            if(user == null){
                user = new LoginUser();
                user.setUserId(1L);
            }

            yqZtlx.setCreatorId(user.getUserId());
            yqZtlx.setCreateTime(DateUtils.getNowDate());
            if(yqZtlxMapper.insertYqZtlx(yqZtlx)>0){
                successMsg.append("添加成功！");
            };
        }
//        return num;
        return successMsg.toString();
    }

    /**
     * 修改载体类型
     * 
     * @param yqZtlx 载体类型
     * @return 结果
     */
    @Override
    public String updateYqZtlx(YqZtlx yqZtlx)
    {
        int num=0;
        StringBuilder successMsg = new StringBuilder();
        String userCode = SecurityUtils.getTenantCode();
        String ztTable = "";
        String pggTable = "";
        if(StringUtils.isEmpty(userCode)){
            ztTable = "YQZT";
            pggTable = "YQPGG";
        }else {
//                ztTable = userCode.toUpperCase()+"_YQZT";
//                pggTable = userCode.toUpperCase()+"_YQPGG";

            ztTable = "YQZT";
            pggTable = "YQPGG";
        }
        if (yqZtlxMapper.selectZtlxInZt(ztTable,yqZtlx.getZtlxId())>0||yqZtlxMapper.selectZtlxInPgg(pggTable,yqZtlx.getZtlxId())>0){
            successMsg.append("名称为：“"+yqZtlx.getZtlxName()+"”的载体类型已被使用，无法进行修改！");
        }else {
            if (yqZtlxMapper.isOnly(yqZtlx)!=0){
                successMsg.append("名称：”"+yqZtlx.getZtlxId()+"”已存在！");
            }else {
                LoginUser user = SecurityUtils.getLoginUser();

                if(user == null){
                    user = new LoginUser();
                    user.setUserId(1L);
                }

                yqZtlx.setLastupdater(user.getUserId());
                yqZtlx.setLastupdateTime(DateUtils.getNowDate());
                if(yqZtlxMapper.updateYqZtlx(yqZtlx)>0){
                    successMsg.append("修改成功！");
                };
            }
        }
//        return num;
        return successMsg.toString();
    }

    /**
     * 批量删除载体类型
     * 
     * @param ztlxIds 需要删除的载体类型主键
     * @return 结果
     */
    @Override
    public String deleteYqZtlxByZtlxIds(Long[] ztlxIds)
    {
        StringBuilder failMsg = new StringBuilder();
        int successNum = 0;
        int failNum = 0;
        YqZtlx yqZtlx = new YqZtlx();
        for (int i = 0; i < ztlxIds.length; i++) {
            String userCode = SecurityUtils.getTenantCode();
            String ztTable = "";
            String pggTable = "";
            if(StringUtils.isEmpty(userCode)){
                ztTable = "YQZT";
                pggTable = "YQPGG";
            }else {
//                ztTable = userCode.toUpperCase()+"_YQZT";
//                pggTable = userCode.toUpperCase()+"_YQPGG";

                ztTable = "YQZT";
                pggTable = "YQPGG";
            }
            if (yqZtlxMapper.selectZtlxInZt(ztTable,ztlxIds[i])>0||yqZtlxMapper.selectZtlxInPgg(pggTable,ztlxIds[i])>0){
                yqZtlx.setZtlxId(ztlxIds[i]);
                failMsg.append("名称为：\""+yqZtlxMapper.selectYqZtlxByZtlxId(yqZtlx).getZtlxName()+"\"的载体类型已被使用，无法进行删除！\n");
                failNum++;
            }else {
                yqZtlxMapper.deleteYqZtlxByZtlxId(yqZtlx.getTableName(),ztlxIds[i]);
                successNum++;
            }
        }
        if (failNum==0){
            return "删除成功"+successNum+"条！";
        }else {
            return "删除成功"+successNum+"条，删除失败"+failNum+"条。\n失败原因："+failMsg;
        }
    }

    /**
     * 删除载体类型信息
     * 
     * @param ztlxId 载体类型主键
     * @return 结果
     */
    @Override
    public int deleteYqZtlxByZtlxId(Long ztlxId)
    {
        YqZtlx yqZtlx = new YqZtlx();
        return yqZtlxMapper.deleteYqZtlxByZtlxId(yqZtlx.getTableName(),ztlxId);
    }
}
